﻿/****************************************************************
*   作者：Morain
*   创建时间：2018/2/9 21:05:20
*   描述说明：
*****************************************************************/
using System;
using System.Diagnostics;

namespace Model
{
  [ObjectSystem]
  public class TestCompnentSystem : ObjectSystem<TestComponent>, IAwake, IUpdate, IStart
  {
    public void Awake()
    {

    }

    public void Start()
    {
      // TestBuffer();
      //TestConfig();
      //TestMessage();
    }

    private static void TestMessage()
    {
      C2G_Login c2 = new C2G_Login
      {
        UserName = "100000000000000000000000000000000000000000000000000000000000000000",
        Password = "100000000000000000000000000000000000000000000000000000000000000000"
      };

      byte[] b = ProtobufHelper.ToBytes(c2);
      Type type = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(Opcode.C2G_LONG);

      IMessage message = (IMessage)ProtobufHelper.FromBytes(type, b);
      ushort len = (ushort)(b.Length + 1 + 2);

      Log.Info($"[{BitConverter.GetBytes(len).ToHex2()} 00 {BitConverter.GetBytes(Opcode.C2G_LONG).ToHex2()} {b.ToHex2()}]");
    }

    private static void TestConfig()
    {
      StartConfig all = new StartConfig();
      all.AppId = 0;
      all.AppType = AppType.All;
      OuterConfig outerConfig = all.AddComponent<OuterConfig>();
      outerConfig.Host = "127.0.0.1";
      outerConfig.Port = 8000;

      InnerConfig innerConfig = all.AddComponent<InnerConfig>();
      innerConfig.Host = "127.0.0.1";
      innerConfig.Port = 8001;

      string bson = MongoHelper.ToJson(all);
      var test = MongoHelper.FromJson<StartConfig>(bson);
      Log.Info(bson);
    }

    private static void TestBuffer()
    {
      RingBuffer ringBuffer = new RingBuffer();
      Stopwatch stopwatch = new Stopwatch();
      stopwatch.Start();
      byte[] bytes = { 1, 2, 3, 4, 5 };
      for (int i = 0; i < 10000000; i++)
      {
        ringBuffer.SendTo(bytes);
      }
      stopwatch.Stop();

      Log.Info("compute..." + stopwatch.ElapsedMilliseconds + "ms");

      stopwatch.Restart();

      for (int i = 0; i < 10000000; i++)
      {
        byte[] buffer = new byte[5];
        ringBuffer.RecvFrom(buffer, 5);
      }
      stopwatch.Stop();
      Log.Info("compute..." + stopwatch.ElapsedMilliseconds + "ms");
    }

    public void Update()
    {

    }
  }

  public class TestComponent : Component
  {
    public int count;
  }
}